﻿using System;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// 角色系统
/// </summary>
public class CharacterSystem:IGameSystem
{
    // 敌人列表
    private List<ICharacter> mEnemys = new List<ICharacter>();
    
    // 士兵列表
    private List<ICharacter> mSoldiers = new List<ICharacter>();
    
    // 添加敌人
    public void AddEnemy(IEnemy enemy)
    {
        mEnemys.Add(enemy);
    }
    
    // 移除敌人
    public void RemoveEnemey(IEnemy enemy)
    {
        mEnemys.Remove(enemy);
    }
    
    // 添加士兵
    public void AddSoldier(ISoldier soldier)
    {
        mSoldiers.Add(soldier);
    }
    
    // 移除士兵
    public void RemoveSoldier(ISoldier soldier)
    {
        mSoldiers.Remove(soldier);
    }
    
    // 更新角色系统
    public override void Update()
    {
        UpdateEnemy(); // 更新敌人
        UpdateSoldier(); // 更新士兵
        
        // 移除死亡角色
        RemoveCharacterIsKilled(mEnemys);
        RemoveCharacterIsKilled(mSoldiers);
    }
    
    // 更新敌人
    private void UpdateEnemy()
    {
        foreach (IEnemy e in mEnemys)
        {
            e.Update();
            e.UpdateFSMAI(mSoldiers);
        }
    }
    
    // 更新士兵
    private void UpdateSoldier()
    {
        foreach (ISoldier s in mSoldiers)
        {
            s.Update();
            s.UpdateFSMAI(mEnemys);
        }
    }
    
    // 移除死亡角色
    private void RemoveCharacterIsKilled(List<ICharacter> characters)
    {
        List<ICharacter> canDestroyes = new List<ICharacter>();
        foreach (ICharacter character in characters)
        {
            if (character.canDestroy)
            {
                canDestroyes.Add(character);
            }
        }
        foreach (ICharacter character in canDestroyes)
        {
            character.Release();
            characters.Remove(character);
        }
    }
    
    // 访问者模式：统计信息
    public void RunVisitor(ICharacterVisitor visitor)
    {
        foreach (ICharacter character in mEnemys)
        {
            character.RunVisitor(visitor);
        }
        foreach (ICharacter character in mSoldiers)
        {
            character.RunVisitor(visitor);
        }
    }
}
